<?php

include_once(dirname(__FILE__)."/filesystemio.php");
include_once(dirname(__FILE__)."/user.php");

class FileSystem
{
    protected $_username;
    protected $_status;
    public $LOCATION = array(
        "home" => User::STATUS_HOME,
        "work" => User::STATUS_WORK,
        "public" =>User::STATUS_PUBLIC,
    );

    public function __construct($username, $status)
    {
        $this->_username = $username;
        $this->_status  = $status;
        $this->_basedir = dirname(__FILE__)."/../data/".$this->_username;
        $this->_fs = new FileSystemIO();
    }
   
    # called at the user first login (after registeration)
    public function initDirectory()
    {
        $basedir = $this->_basedir;
        $this->_fs->mkdir($basedir);
        $this->_fs->mkdir("$basedir/work");
        $this->_fs->mkdir("$basedir/home");
        $this->_fs->mkdir("$basedir/public");
    } 
    
    //set a directory
    public function makeDirectory($path)
    {
    	$this->_fs->mkdir($path);
    }


    # list files of given directory
    # input : relative path to basedir 
    # output: array of files or -1 failure
    public function listDirectory($path)
    {
        #if ($this->_status)
        $strDir = $this->_basedir."/".$path;
        return $this->_fs->ls($strDir);
    }

    # download a file
    # input : file path string 
    # output: 0 success or -1 failure
    public function downloadFile($strFile)
    {
        return 0;
    }


    # upload a file
    # output: 0 success or -1 failure 
    # uploadFile to current directory
    public function uploadFile($strDir)
    {
        # uploaded file should stored in $_FILES["file"] 
        foreach ($_FILES as $file)
        {
            $filename = $file["name"];
            $filepath = $this->_basedir."/$strDir/$filename";
            # move file from tmp to demo_dir
            move_uploaded_file($file["tmp_name"], $filepath); 
            $this->_fs->add($filepath);
        }
    }

    public function deleteFile($strFilePath)
    {
        $strFilePath = $this->_basedir."/$strFilePath";
        if (is_dir($strFilePath))
        {
            $this->_fs->rmdir($strFilePath);
        }
        else
        {
            $this->_fs->rm($strFilePath);
        }
    }

    # move file from src dir to dest dir 
    public function move($srcFile, $destFile)
    {
        $srcFile  = $this->_basedir."/$srcFile";
        $destFile = $this->_basedir."/$destFile";
        $this->_fs->move($srcFile, $destFile);
    }
    
    # copy file from src dir to dest dir 
    public function copy($srcFile, $destFile)
    {
        $srcFile  = $this->_basedir."/$srcFile";
        $destFile = $this->_basedir."/$destFile";
        $this->_fs->copy($srcFile, $destFile);
    }

};


